package com.app.biz.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.app.biz.domain.BizProvincialAdvancedManufacturingCluster;
import com.app.biz.domain.BizTopHundredComprehensiveEnterprise;
import com.app.biz.mapper.BizTopHundredComprehensiveEnterpriseMapper;
import com.app.biz.service.IBizTopHundredComprehensiveEnterpriseService;
import com.app.common.core.domain.AjaxResult;
import com.app.common.utils.DateUtils;
import com.app.common.utils.SecurityUtils;
import com.app.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BizTopHundredComprehensiveEnterpriseServiceImpl implements IBizTopHundredComprehensiveEnterpriseService {

    @Autowired
    private BizTopHundredComprehensiveEnterpriseMapper bizTopHundredComprehensiveEnterpriseMapper;


    @Override
    public BizTopHundredComprehensiveEnterprise selectBizTopHundredComprehensiveEnterpriseByBaseId(String baseId) {
        return bizTopHundredComprehensiveEnterpriseMapper.selectBizTopHundredComprehensiveEnterpriseByBaseId(baseId);
    }

    @Override
    public List<BizTopHundredComprehensiveEnterprise> selectBizTopHundredComprehensiveEnterpriseList(BizTopHundredComprehensiveEnterprise bizTopHundredComprehensiveEnterprise) {
        return bizTopHundredComprehensiveEnterpriseMapper.selectBizTopHundredComprehensiveEnterpriseList(bizTopHundredComprehensiveEnterprise);
    }

    @Override
    public int insertBizTopHundredComprehensiveEnterprise(BizTopHundredComprehensiveEnterprise bizTopHundredComprehensiveEnterprise) {

        bizTopHundredComprehensiveEnterprise.setBaseId(IdUtil.fastUUID());
        bizTopHundredComprehensiveEnterprise.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        bizTopHundredComprehensiveEnterprise.setCreateTime(new Date());
        return bizTopHundredComprehensiveEnterpriseMapper.insertBizTopHundredComprehensiveEnterprise(bizTopHundredComprehensiveEnterprise);
    }

    @Override
    public int updateBizTopHundredComprehensiveEnterprise(BizTopHundredComprehensiveEnterprise bizTopHundredComprehensiveEnterprise) {
        bizTopHundredComprehensiveEnterprise.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        bizTopHundredComprehensiveEnterprise.setUpdateTime(new Date());
        return bizTopHundredComprehensiveEnterpriseMapper.updateBizTopHundredComprehensiveEnterprise(bizTopHundredComprehensiveEnterprise);
    }

    @Override
    public int deleteBizTopHundredComprehensiveEnterpriseByBaseIds(String[] baseIds) {
        return bizTopHundredComprehensiveEnterpriseMapper.deleteBizTopHundredComprehensiveEnterpriseByBaseIds(baseIds);
    }

    @Override
    public int deleteBizTopHundredComprehensiveEnterpriseByBaseId(String baseId) {
        return bizTopHundredComprehensiveEnterpriseMapper.deleteBizTopHundredComprehensiveEnterpriseByBaseId(baseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(String absoluteExcelPath, String year) {
        List<BizTopHundredComprehensiveEnterprise> excelDataArgs = EasyExcel.read(absoluteExcelPath).head(BizTopHundredComprehensiveEnterprise.class).headRowNumber(1).sheet().doReadSync();
        if(!CollectionUtils.isEmpty(excelDataArgs)){
            excelDataArgs = excelDataArgs.stream().filter(f-> !StringUtils.isEmpty(f.getName())).collect(Collectors.toList());
        }
        if(!CollectionUtils.isEmpty(excelDataArgs)){
            for(BizTopHundredComprehensiveEnterprise item : excelDataArgs){
                item.setFillYear(year);
                item.setCreateTime(DateUtils.getNowDate());
                item.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                item.setBaseId(IdUtil.simpleUUID());
            }
            bizTopHundredComprehensiveEnterpriseMapper.batchInsertBizTopHundredComprehensiveEnterprise(excelDataArgs);
        }else{
            return AjaxResult.success("上传文档内容不能为空");
        }

        return AjaxResult.success();

    }

    
}
